<?php

/**
 * Description of PanelView
 *
 * @author Ian Clarence <ian.clarence@gmail.com>
 */
final class PanelView extends ViewPanel {
    
    /**
     * The constructor.
     */
    public function __construct($fields = array()) {
        parent::__construct($fields);        
    }
    
    /**
     * Instance of an article.
     * 
     * @param array $article
     * @return mixed
     */
    protected function getArticleInstance($article) {
        return $this->genArticleView($this->getArticleFields($article));
    }
    
    /**
     * The final rendering. 
     */
    public function make() {
        parent::make();
        $panelTypes = ViewUtils::getPanelTypes();
        $article = $this->fields['page']['article'];

        /**
         * Normal running mode.
         */
        if (DEBUGGING_MODE == OFF) {
            
            /**
             * If only one article is viewed (specified in the URL). 
             */
            if ($this->fields['panel']['panel_type'] == CONTENT_PANEL_TYPE && is_array($article)) {
                $articleView = $this->getArticleInstance($article);
                $articleView->make();
                $this->addInnerHtml($articleView);
            }
            
            /**
             * Viewing all articles as normal. 
             */
            else {
                if (count($this->fields['panel']['articles']) > 0) {
                    foreach ($this->fields['panel']['articles'] as $articleKey => $article) {
                        $articleView = $this->getArticleInstance($article);
                        $articleView->make();
                        $this->addInnerHtml($articleView);
                    }
                }
                else {                
                    $this->addInnerHtml(ViewHtmlElement::textInit(''));
                }
            }
        }
    }

}